package com.huazi.vod.service;



import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.huazi.vod.utils.ConstantVodUtils;
import com.huazi.vod.utils.InitObject;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author shkstart
 * @create 2021-03-22 22:42
 */
@Service
public class VodServiceImpl implements VodService {
    //上传视频
    @Override
    public String uploadVideo(MultipartFile file) {
        //accessKeyId、accessKeySecret
        String accessKeyId= ConstantVodUtils.KEY_ID;
        String accessKeySecret=ConstantVodUtils.KEY_SECRET;
        String fileName=file.getOriginalFilename();
        String title = fileName.substring(0,fileName.lastIndexOf("."));
        try {
            UploadStreamRequest request=new UploadStreamRequest(accessKeyId,accessKeySecret,title,fileName,file.getInputStream());
            UploadVideoImpl uploadVideo = new UploadVideoImpl();
            UploadStreamResponse response = uploadVideo.uploadStream(request);
            String videoId=null;
            if(response.isSuccess()){
                videoId=response.getVideoId();
            }else{
                videoId=response.getVideoId();
            }

            return videoId;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    //删除视频
    @Override
    public void deleteVodVideo(String id) {
        DefaultAcsClient client= null;
        DeleteVideoResponse response = new DeleteVideoResponse();
        try {
            client = InitObject.initVodClient(ConstantVodUtils.KEY_ID,ConstantVodUtils.KEY_SECRET);
            DeleteVideoRequest request=new DeleteVideoRequest();
            //可以删除多个视频，之间，号隔开
            request.setVideoIds(id);

            response=client.getAcsResponse(request);
        } catch (ClientException e) {
            System.out.println("ErrorMessage="+e.getLocalizedMessage());
        }
        System.out.println("RequestId="+response.getRequestId());
    }

}
